﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Google.search;
using System.Net;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace Google.search
{
    public struct SearchType
    {
        public string url;
        public string title;
        public string content;
        public FindingEngine engine;
        public enum FindingEngine { Google, Bing, GoogleAndBing };
    }

    public interface ISearchResult
    {
        SearchType.FindingEngine Engine { get; set; }
        string SearchExpression { get; set; }
        List<SearchType> Search();
    }

    public class GoogleSearch : ISearchResult
    {
        public GoogleSearch(string searchExpression)
        {
            this.Engine = SearchType.FindingEngine.Google;
            this.SearchExpression = searchExpression;
        }
        public SearchType.FindingEngine Engine { get; set; }
        public string SearchExpression { get; set; }

        public List<SearchType> Search()
        {
            //const string urlTemplate = @"http://ajax.googleapis.com/ajax/services/search/web?v=1.0& \ rsz=large&safe=active&q={0}&start={1}";
            const string urlTemplate = @"http://ajax.googleapis.com/ajax/services/search/web?v=1.0& \ rsz=large&safe=active&q={0}";
            var resultsList = new List<SearchType>();
            int[] offsets = { 0, 8, 16, 24, 32, 40, 48 };
            foreach (var offset in offsets)
            {
                var searchUrl = new Uri(string.Format(urlTemplate, SearchExpression, offset));
                WebClient wc = new WebClient();
                wc.Proxy = WebRequest.DefaultWebProxy;
                wc.Credentials = System.Net.CredentialCache.DefaultCredentials;
                wc.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

                //var page = new WebClient().DownloadString(searchUrl);
                var page = wc.DownloadString(searchUrl);
                var o = (JObject)JsonConvert.DeserializeObject(page);

                var resultsQuery =
                  from result in o["responseData"]["results"].Children()
                  select new SearchType
                  {
                      url = result.Value<string>("url").ToString(),
                      title = result.Value<string>("title").ToString(),
                      content = result.Value<string>("content").ToString(),
                      engine = this.Engine
                  };

                resultsList.AddRange(resultsQuery);
            }
            return resultsList;
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        //ISearchResult searchClass = new BingSearch("Asp.net mvc");
        ISearchResult searchClass = new GoogleSearch("relogio");
        var list = searchClass.Search();
        foreach (var searchType in list)
        {
            Console.WriteLine(searchType.title);
        }
        Console.Read();
    }
}
